#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System.Collections.Generic;
using Neatly.Module;
using UnityEngine;

namespace Neatly.Load.Pool
{
    public class LoaderPool : IPoolBase<Loader>
    {
        private const int SECOND_ONE = 1;
        private float m_RecordTime = 0;
        //总loader
        private readonly Dictionary<string, Loader> m_AllLoaders = new();
        //临时loader
        private readonly List<Loader> m_TempLoader = new();
        //总AtlasLoader
        private readonly Dictionary<string, Loader> m_AllAtlasLoaders = new();

        public void Add(string name, Loader loader)
        {
            loader.LiveTime = 0;
            //是否图集bundle
            if (loader.IsAtlas)
            {
                if (m_AllAtlasLoaders.Remove(loader.AtlasName, out var beforeLoader))
                {
                    loader.RefCount += beforeLoader.RefCount;
                }
                m_AllAtlasLoaders.Add(loader.AtlasName, loader);
            }
            if (!loader.Persistent)
            {
                m_TempLoader.Add(loader);
            }
            m_AllLoaders.TryAdd(name, loader);
        }

        public Loader Get(string key, bool isDependency)
        {
            m_AllLoaders.TryGetValue(key, out var loader);
            //非常驻,非图集
            if (loader != null && isDependency)
            {
                loader.AddDependCount();
            }
            return loader;
        }

        //获取AtlasLoader
        public Loader GetAtlasLoader(string key)
        {
            m_AllAtlasLoaders.TryGetValue(key, out var loader);
            return loader;
        }

        public void LoseRef(string assetName)
        {
            if (string.IsNullOrEmpty(assetName))
            {
                return;
            }
            foreach (var loader in m_TempLoader)
            {
                if (loader.ContainAsset(assetName))
                {
                    loader.LoseReferenceCount();
                    break;
                }
            }
        }

        //增加Atlas引用
        public void AddAtlasRef(string key)
        {
            if (m_AllAtlasLoaders.TryGetValue(key, out var loader))
            {
                loader.AddReferenceCount();
            }
        }

        //解除Atlas引用
        public void LoseAtlasRef(string key)
        {
            if (m_AllAtlasLoaders.TryGetValue(key, out var loader))
            {
                loader.LoseReferenceCount();
            }
        }

        public void Update()
        {
            //定时清理资源
            // m_RecordTime += Time.deltaTime;
            // if (m_RecordTime < SECOND_ONE) return;
            // m_RecordTime -= SECOND_ONE;
            // var curDeltaTime = Time.realtimeSinceStartup - NeatlyConfig.TIME_BUNDLE_CACHE;
            // //清理临时bundle
            // for (int i = m_TempLoader.Count - 1; i >= 0; i--)
            // {
            //     var loader = m_TempLoader[i];
            //     if (loader.RefCount > 0 || loader.DependCount > 0) continue;
            //     if (!(loader.LiveTime <= curDeltaTime)) continue;
            //     if (NeatlyConfig.AssetBundleEnable)
            //     {
            //         string[] dependencies = LoadModule.Instance.GetAllDependencies(loader.Path);
            //         //处理依赖关联资源引用计数
            //         foreach (var dependName in dependencies)
            //         {
            //             m_AllLoaders.TryGetValue(dependName, out var dependLoader);
            //             if (dependLoader != null)
            //             {
            //                 m_AllLoaders[dependName].LoseDependCount();
            //             }
            //         }
            //         loader.Unload(true);
            //     }
            //     if (loader.IsAtlas)
            //     {
            //         m_AllAtlasLoaders.Remove(loader.AtlasName);
            //     }
            //     m_AllLoaders.Remove(loader.Path);
            //     m_TempLoader.RemoveAt(i);
            //     return;
            // }
        }

        public void Remove(string key)
        {
        }

        public void ClearVersion()
        {
            foreach (var kv in m_AllLoaders)
            {
                kv.Value.Unload(false);
            }
            m_AllLoaders.Clear();
            m_TempLoader.Clear();
        }

        #region 可视化接口
        public Dictionary<string, Loader> GetAllLoader()
        {
            return m_AllLoaders;
        }
        #endregion
    }
}